Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not configure the root logger. #741

Closed
wants to merge 2 commits into from
Closed

Do not configure the root logger. #741

wants to merge 2 commits into from

Conversation

schlamar
Copy link
Contributor

Calling basicConfig leaks to other custom defined loggers, so this leads to undesirable behavior. For example in this code example log messages gets printed out twice:

logger = logging.getLogger('mylogger')
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)

loop = ioloop.IOLoop.current()
loop._stopped = True
loop.start()
logger.info('Test')

This PR changes the check of handlers by adding only the basicConfig handlers to the Tornado loggers.

@bdarnell
Copy link
Member

The root logger is implicitly configured by functions like logging.warning; you shouldn't rely on the root logger remaining unconfigured. Instead, if you attach a handler to a logger that would be redundant with a root handler, set the propagate attribute of that logger to false: http://docs.python.org/2/library/logging.html#logging.Logger.propagate

@schlamar
Copy link
Contributor Author

Ah, thanks for that :)

However, I would still discourage usage of logging.basicConfig because it could be called from within a thread and you shouldn't do that...

@bdarnell
Copy link
Member

Yes, but given the existence of logging.warning and friends, all multithreaded apps should explicitly configure their loggers before starting any threads unless they're sure that these functions are never used.

@schlamar
Copy link
Contributor Author

Agreed. Thanks again for the propagate hint :)

@schlamar schlamar closed this Apr 18, 2013
@schlamar schlamar deleted the fix-log-issue branch April 18, 2013 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants